﻿using AnQinFrameWorkAPI.DataBase;
using AnQinFrameWorkAPI.Entity.SystemCore.DTO.RoleButton;
using AnQinFrameWorkAPI.Entity.SystemCore.Entity;
using AnQinFrameWorkAPI.Entity.SystemCore.View.RoleButton;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;

namespace AnQinFrameWorkAPI.Service.SystemCore;

/// <summary>
///     角色按钮业务层
/// </summary>
public class RoleButtonService
{
    #region 获取角色按钮列表

    public async Task<RequestResponseModel> GetRoleButtonList(RoleButtonSearchDTO roleButtonSearchDto)
    {
        var roleId = Guid.Parse(roleButtonSearchDto.RoleId);
        var menuId = Guid.Parse(roleButtonSearchDto.MenuId);

        var existingRoleMenuIds = await _context.RoleButton
            .Where(rm => rm.RoleId == roleId)
            .Where(a => a.MenuId == menuId)
            .Select(rm => rm.MenuButtonId)
            .ToListAsync();

        var addRoleButtonList = await _context.MenuButton
            .Where(m => !existingRoleMenuIds.Contains(m.Id))
            .Select(m => new RoleButtonModel
            {
                Id = Guid.NewGuid(),
                RoleId = roleId,
                MenuId = menuId,
                MenuButtonId = m.Id,
                IsEnable = false
            })
            .ToListAsync();

        if (addRoleButtonList.Count > 0)
        {
            _context.RoleButton.AddRange(addRoleButtonList);
            await _context.SaveChangesAsync();
        }

        var query = _context.RoleButton
            .ProjectTo<RoleButtonView>(_mapper.ConfigurationProvider)
            .AsNoTracking().Where(a => a.RoleId.ToString() == roleButtonSearchDto.RoleId);

        //返回列表
        var roleButtonList = await query
            .Page(roleButtonSearchDto.PageIndex, roleButtonSearchDto.PageSize)
            .ToListAsync();

        //总数据量
        var pageCount = await query.CountAsync();

        //返回数据
        var pageResult = new PageResult<RoleButtonView>(roleButtonList, pageCount);
        return RequestResponse.OK(pageResult, "查询成功");
    }

    #endregion

    #region 启用角色按钮

    public async Task<RequestResponseModel> EnableMultiRoleButton(RoleButtonUpdateDTO roleButtonUpdateDto)
    {
        //查询角色按钮
        var query = await _context.RoleButton
            .Where(a => a.RoleId.ToString() == roleButtonUpdateDto.RoleId)
            .Where(a => a.MenuId.ToString() == roleButtonUpdateDto.MenuId)
            .Where(a => roleButtonUpdateDto.ButtonId.Contains(a.MenuButtonId.ToString()))
            .ToListAsync();

        if (query.Count != roleButtonUpdateDto.ButtonId.Count)
            return RequestResponse.ErrorRequest("角色Id无效或者菜单id无效或者按钮列表中有按钮ID无效");

        //启用
        foreach (var item in query) item.IsEnable = true;

        _context.RoleButton.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(roleButtonUpdateDto.ButtonId, "启用成功");
    }

    #endregion

    #region 禁用角色按钮

    public async Task<RequestResponseModel> DisableMultiRoleButton(RoleButtonUpdateDTO roleButtonUpdateDto)
    {
        //查询角色按钮
        var query = await _context.RoleButton
            .Where(a => a.RoleId.ToString() == roleButtonUpdateDto.RoleId)
            .Where(a => a.MenuId.ToString() == roleButtonUpdateDto.MenuId)
            .Where(a => roleButtonUpdateDto.ButtonId.Contains(a.MenuButtonId.ToString()))
            .ToListAsync();

        if (query.Count != roleButtonUpdateDto.ButtonId.Count)
            return RequestResponse.ErrorRequest("角色Id无效或者菜单id无效或者按钮列表中有按钮ID无效");

        //启用
        foreach (var item in query) item.IsEnable = false;

        _context.RoleButton.UpdateRange(query);
        await _context.SaveChangesAsync();
        return RequestResponse.OK(roleButtonUpdateDto.ButtonId, "禁用成功");
    }

    #endregion

    #region 初始化

    //数据库
    private readonly DatabaseContext _context;

    //autoMapper
    private readonly IMapper _mapper;

    //构造注入
    public RoleButtonService(DatabaseContext context, IMapper mapper)
    {
        _context = context;
        _mapper = mapper;
    }

    #endregion
}